﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SaleOnlineSystem.Admin.Controls
{
    public partial class EditNews : System.Web.UI.UserControl
    {
        public int newsid;
        public string title;
        public string description;
        public string decodenoidung;
        public string image;
        public string date;
        public string summary;
        public string filePathNewsIamge;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                newsid = Convert.ToInt32(Request.QueryString["newsid"]);
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_News", "*", "News_ID", newsid, "selectwhereid");
                description = dt.Rows[0]["News_Description"].ToString();
                decodenoidung = Server.HtmlDecode(description);
                summary = dt.Rows[0]["News_Summary"].ToString();
                image = dt.Rows[0]["News_Image"].ToString();
                title = dt.Rows[0]["News_Name"].ToString();
                date = dt.Rows[0]["News_Date"].ToString();
                txtTitle.Text = title;
                CKEditorNoidung.Text = decodenoidung;
                txtSummary.Text = summary;
            }
            catch (Exception)
            {
                Response.Redirect("/error.htm");
            }
        }

        protected void btnUpdateNews_Click(object sender, EventArgs e)
        {
            if (txtTitle.Text != "")
            {
                if (txtSummary.Text != "")
                {
                    if (CKEditorNoidung.Text != "")
                    {
                        if (fulImageNews.HasFile)
                        {
                            if (LIB.Validate.CheckFileType(fulImageNews.FileName))
                            {
                                LIB.Validate saveimage = new LIB.Validate();
                                filePathNewsIamge = "/Images/Image-News/" + fulImageNews.FileName + "";
                                saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Images/Image-News/"), fulImageNews.FileName, 200, fulImageNews.PostedFile.InputStream);
                                File.Delete(MapPath(image));
                                DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[5];
                                cl[0] = new DAL.DHelpers.Column("News_Image", filePathNewsIamge, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                                cl[1] = new DAL.DHelpers.Column("News_Name", txtTitle.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                                cl[2] = new DAL.DHelpers.Column("News_Summary", txtSummary.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                                cl[3] = new DAL.DHelpers.Column("News_Description", CKEditorNoidung.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                                cl[4] = new DAL.DHelpers.Column("News_Date", DateTime.Now.ToString(), DAL.DHelpers.DataType.DataTypeSql.Nvarchar);

                                string[] colWhere = new string[1];
                                colWhere[0] = "News_ID";

                                object[] colValue = new object[1];
                                colValue[0] = newsid;

                                DAL.DCollector.DBCollector.Instance.Update("tbl_News", cl, colWhere, colValue);
                                Response.Redirect("/Admin/Index.aspx?panel=7&sub=10");
                            }
                            else
                            {
                                Response.Write("<script>alert('Chỉ có thể upload file(.jpg,.png,.jpeg)')</script>");
                            }
                        }
                        else
                        {
                            DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[4];
                            cl[0] = new DAL.DHelpers.Column("News_Name", txtTitle.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[1] = new DAL.DHelpers.Column("News_Summary", txtSummary.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[2] = new DAL.DHelpers.Column("News_Description", CKEditorNoidung.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[3] = new DAL.DHelpers.Column("News_Date", DateTime.Now.ToString(), DAL.DHelpers.DataType.DataTypeSql.Nvarchar);

                            string[] colWhere = new string[1];
                            colWhere[0] = "News_ID";

                            object[] colValue = new object[1];
                            colValue[0] = newsid;

                            DAL.DCollector.DBCollector.Instance.Update("tbl_News", cl, colWhere, colValue);
                            Response.Redirect("/Admin/Index.aspx?panel=7&sub=10");
                        }
                    }
                    else
                    {
                        Response.Write("<script>alert('Bạn vui lòng nhập nội dung tin tức')</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('Bạn vui lòng nhập tóm tắt nội dung')</script>");
                }

            }
            else
            {
                Response.Write("<script>alert('Bạn vui lòng nhập tiêu đề tin tức')</script>");
            }
        }
    }
}